<html lang="zh-cn">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Bootstrap demo</title>
      <link href="/static/bootstrap/css/bootstrap.css" rel="stylesheet" >
      <script src="/static/bootstrap/js/bootstrap.bundle.min.js" ></script>
  </head>
  <body>

   {% include "search/header.html"%}

<div class="container">
  <h1>查询分类帐</h1>
  <p></p>

</div>

   <div class="container-sm">
<!-- 搜索表单 -->
        <form method="get" action="{% url 'ledger' %}" class="search-form d-flex align-items-center gap-2 mb-3">
            <select  class="col-2" class="form-select " style="padding-top: 3px;margin-top: 1px;margin-bottom: 1px" name="select_type">
              <option value="0" {% if request.GET.select_type == '0' %}selected{% endif %}>根据货位显示</option>
              <option value="1" {% if request.GET.select_type == '1' %}selected{% endif %} >根据批号显示</option>
            </select>
              <input class="col-auto" type="date" class="form-control" name="begin_date"  value="{% if request.GET.begin_date %}{{ request.GET.begin_date }}{% else %}{{ today|date:'Y-m-01' }}{% endif %}" >
        <span >-</span>
        <input  class="col-auto" type="date" class="form-control" name="after_date"  value="{% if request.GET.after_date %}{{ request.GET.after_date }}{% else %}{{ today|date:'Y-m-t' }}{% endif %}" >
            <input class="col-2" type="text" name="q" placeholder="搜索品名"
                   value="{{ request.GET.q|default:'' }}">
            <button type="submit">搜索</button>
            {% if request.GET.q %}
                <a href="{% url 'ledger' %}">清除搜索</a>
            {% endif %}
        </form>

 <!-- 展示列表数据 -->
    <div class="table-responsive">
    <table class="table ">
        <thead>
            <tr>
                <!-- 循环模型的字段，排除自动生成的id -->
                  {% for field in model_fields %}
                    {% if field.name != 'id' %}  <!-- 排除id字段 -->
                      <th>{{ field.verbose_name }}</th>
                    {% endif %}
                  {% endfor %}
                    <th>操作</th>
            </tr>
        </thead>
        <tbody>
           {% for values in item_values_list %}  <!-- 遍历值列表 -->
                <tr>
                    {% for value in values %}  <!-- 遍历每个对象的字段值 -->
                        {% if not forloop.first %}
                            <td>{{ value }}</td>
                        {% endif %}

                    {% endfor %}
                    <td>

                     <a href="/search/store/store{% if values.1 == '原料' %}raw{% elif values.1 == '包材' %}packaging{% elif values.1 == '添加剂' %}additive{% elif values.1 == '中间品' %}intermediate{% else %}finished{% endif %}detailmodel/subledger/?pk={{ values.0 }}&select_type={{ request.GET.select_type }}" target="_blank">查看明细</a>

                    </td>
                </tr>

                {% empty %}
                <tr>
                    <td colspan="4">暂无数据</td>
                </tr>
                {% endfor %}
        </tbody>
    </table>
    </div>
    <!-- 分页（如果使用 ListView 并设置了 paginate_by） -->
    {% if is_paginated %}
    <ul class="pagination pagination-sm">

        {% if page_obj.has_previous %}
            <li class="page-item">
            <a href="?page=1{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.begin_date %}&begin_date={{ request.GET.begin_date }}{% endif %}{% if request.GET.after_date %}&after_date={{ request.GET.after_date }}{% endif %}{% if request.GET.select_type %}&select_type={{ request.GET.select_type }}{% endif %}" class="page-link"  >首页</a>

            <li class="page-item" >
            <a href="?page={{ page_obj.previous_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.begin_date %}&begin_date={{ request.GET.begin_date }}{% endif %}{% if request.GET.after_date %}&after_date={{ request.GET.after_date }}{% endif %}{% if request.GET.select_type %}&select_type={{ request.GET.select_type }}{% endif %}" class="page-link">上一页</a>
            </li>
        {% endif %}
        <li class="page-item" >
        <span >第 {{ page_obj.number }} 页，共 {{ page_obj.paginator.num_pages }} 页</span>
        </li>
        {% if page_obj.has_next %}
             <li class="page-item" >
            <a class="page-link" href="?page={{ page_obj.next_page_number }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.begin_date %}&begin_date={{ request.GET.begin_date }}{% endif %}{% if request.GET.after_date %}&after_date={{ request.GET.after_date }}{% endif %}{% if request.GET.select_type %}&select_type={{ request.GET.select_type }}{% endif %}">下一页</a>
            </li>
             <li class="page-item" >
            <a class="page-link" href="?page={{ page_obj.paginator.num_pages }}{% if request.GET.q %}&q={{ request.GET.q }}{% endif %}{% if request.GET.begin_date %}&begin_date={{ request.GET.begin_date }}{% endif %}{% if request.GET.after_date %}&after_date={{ request.GET.after_date }}{% endif %}{% if request.GET.select_type %}&select_type={{ request.GET.select_type }}{% endif %}">末页</a>
            </li>
        {% endif %}
        <!-- 跳转到指定页 -->
    <li class="page-item">
        <form method="get" style="display: flex; align-items: center;">
            <!-- 保留当前页面的其他GET参数（如搜索条件） -->
            {% for key, value in request.GET.items %}
                {% if key != 'page' %}
                    <input type="hidden" name="{{ key }}" value="{{ value }}">
                {% endif %}
            {% endfor %}

            <input
                type="number"
                name="page"
                class="form-control form-control-sm mx-1"
                min="1"
                max="{{ page_obj.paginator.num_pages }}"
                value="{{ page_obj.number }}"
                style="width: 60px;"
            >
            <button type="submit" class="btn btn-sm btn-outline-secondary">跳转</button>
        </form>
    </li>
    </ul>
    {% endif %}
   </div>
  </body>
</html>
